fn main() {
    println!("{}", Solution::reverse(123));
    println!("{}", Solution::reverse(-123));
}

struct Solution;

impl Solution {
    pub fn reverse(x: i32) -> i32 {
        let min = i32::MIN / 10;
        let max = i32::MAX / 10;
        let mut num = x;
        let mut res = 0;
        let mut tmp;
        while num != 0 {
            tmp = num % 10;
            // 判断是否会越界
            if res > max || (res == max && tmp > 7) || res < min || (res == min && tmp < -8) {
                return 0;
            }
            res = res * 10 + tmp;
            num = num / 10;
        }
        res
    }
}
